package com.lingx.web.api.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.lingx.model.impl.DefaultEntity;
import com.lingx.model.impl.DefaultMethod;
import com.lingx.service.LingxService;
import com.lingx.service.MethodService;
import com.lingx.service.ModelService;
import com.lingx.web.api.IApi;
@Component
public class Api9016 extends AbstractDevApi implements IApi{
	public static final String SUPER_ADMIN_ID="6e0362e8-100e-11e5-b7ab-74d02b6b5f61";
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private ModelService modelService;
	@Resource
	private LingxService lingxService;
	@Resource
	private MethodService methodService;
	@Override
	public int getApiCode() {
		return 9016;
	}
	@Override
	public String getApiName() {
		return "增删功能树";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> param) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String ecode=IApi.getParamString(param, "ecode", "");
		String mcode=IApi.getParamString(param, "mcode", "");
		String checked=IApi.getParamString(param, "checked", "");
		if("true".equals(checked)) {
			this.addFunc(ecode, mcode);
		}else {
			this.delFunc(ecode, mcode);
		}
		
		return ret;
	}
	
	public void addFunc(String ecode,String mcode) {
		if(ecode.equals(mcode))return;
		insertFunc(this.jdbcTemplate,ecode,mcode);
		
		jdbcTemplate.update("INSERT into tlingx_role_func(id,role_id,func_id)select uuid(),'"+SUPER_ADMIN_ID+"',id from tlingx_func t where not EXISTS (select 1 from tlingx_role_func where role_id='"+SUPER_ADMIN_ID+"' and func_id=t.id)");
		
	}

	public void delFunc(String ecode,String mcode) {
		
		jdbcTemplate.update("delete from tlingx_func where module=? and func=?",ecode,mcode);
		
		if(this.lingxService.queryForInt("select count(*) from tlingx_func where module=?", ecode)==0) {
			this.jdbcTemplate.update("delete from tlingx_func where module=? and func=?",ecode+"_object","-");
		}

	}
	

private boolean insertFunc(JdbcTemplate jdbc,String module, String func) {
	
	String sql = "select count(*) from tlingx_func where module=? and func=?";
	int c =jdbc.queryForObject(sql,Integer.class, new Object[] { module, func });
	if (c == 0) {
		
			
			DefaultEntity se =modelService.getEntity(module); //modelService.get(module);
			String methodName = "";
			if (this.methodService.defaultMethodContains(func)) {
				//methodName = func;
				methodName = methodService.getMethod(func, se).getName();
			} else {
				List<DefaultMethod> list = se.getMethods().getList();
				for (DefaultMethod m : list) {
					if (func.equals(m.getCode())) {
						methodName = m.getName();
						break;
					}
				}
			}
			String fid=getFuncFid(jdbc,module,func);
			String name = String.format("%s-%s", se.getName(), methodName);
			sql = "insert into tlingx_func(id,name,module,func,type,fid)values(?,?,?,?,2,?)";
			jdbc.update(sql,
					new Object[] {String.format("%s-%s", module, func), name, module, func,fid });
		
		return false;
	} else {
		return true;
	}
}

private  String getFuncFid(JdbcTemplate jdbc,String module, String func){
	String sql="select id from tlingx_func where module=? limit 1";
	try {
		return jdbc.queryForObject(sql,String.class,new Object[]{module+"_object"});
	} catch (Exception e) {
		String fid=getObjFid(jdbc,module);
		Map<String,Object> map=jdbc.queryForMap("select name from tlingx_entity where code=?",new Object[]{module});
		jdbc.update("insert into tlingx_func(id,name,module,func,type,fid)values(uuid(),?,?,?,2,?)",new Object[]{map.get("name"),module+"_object","-",fid});

		return jdbc.queryForObject(sql,String.class,new Object[]{module+"_object"});
	}
}
private  String getObjFid(JdbcTemplate jdbc,String module){
	String sql="select id from tlingx_func where module=? limit 1";
	String appId=jdbc.queryForObject("select app_id from tlingx_entity where code=?",String.class,new Object[]{module});
	
	try {
		return jdbc.queryForObject(sql,String.class,new Object[]{appId+"_app"});
	} catch (Exception e) {
		String fid= getAppFid(jdbc);
		Map<String,Object> app=jdbc.queryForMap("select name from tlingx_app where id=?",new Object[]{appId});
		jdbc.update("insert into tlingx_func(id,name,module,func,type,fid)values(uuid(),?,?,?,2,?)",new Object[]{app.get("name"),appId+"_app","-",fid});

		return jdbc.queryForObject(sql,String.class,new Object[]{appId+"_app"});
	}
}
private  String getAppFid(JdbcTemplate jdbc){

	String sql="select id from tlingx_func where module='root_root'";
	try {
		return jdbc.queryForObject(sql,String.class);
	} catch (Exception e) {
		jdbc.update("insert into tlingx_func(id,name,module,func,type,fid)values(uuid(),?,?,?,2,0)",new Object[]{"ROOT","root_root","-"});

		return jdbc.queryForObject(sql,String.class);
	}
}
}
